﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SEO.Model;
using System.Data.SQLite;

namespace SEO.DAL
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2011，Adin Lee
    /// 内容摘要: 长尾关键词表类
    /// 完成日期：2011年6月11日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class RelationKeywordsDAL
    {

        /// <summary>
        /// 获取RelationKeywords表中数据
        /// <returns>返回List</returns>
        /// </summary>
        public List<RelationKeywords> GetAllRelationKeywordsDAL()
        {
            List<RelationKeywords> list = new List<RelationKeywords>();
            string strSql = "select * from RelationKeywords";
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.KADBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    RelationKeywords ks = new RelationKeywords();
                    ks.KeyID = int.Parse(dr["key_id"].ToString());
                    ks.KeyName = dr["key_name"].ToString();
                    ks.IsCatch = int.Parse(dr["is_catch"].ToString());
                    list.Add(ks);
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定RelationKeywords -> " + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 根据指定SQL语句获取RelationKeywords表中数据
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        /// <returns>返回List</returns>
        public List<RelationKeywords> GetRelationKeywordsByWhereDAL(string strSql)
        {
            List<RelationKeywords> list = new List<RelationKeywords>();
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.KADBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    RelationKeywords ks = new RelationKeywords();
                    ks.KeyID = int.Parse(dr["key_id"].ToString());
                    ks.KeyName = dr["key_name"].ToString();
                    ks.IsCatch = int.Parse(dr["is_catch"].ToString());
                    list.Add(ks);
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("根据指定SQL语句获取RelationKeywords表中数据 -> " + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 往RelationKeywords表中插入数据
        /// <param name="ks">实例化RelationKeywords类</param>
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int InsertRelationKeywordsDAL(SEO.Model.RelationKeywords ks)
        {
            int iNum = 0;
            string strSql = "insert into RelationKeywords(key_id,key_name,is_catch) " +
             " values(" + ks.KeyID + ",'" + ks.KeyName + "',0)";
            try
            {
                iNum = SEO.DBSqlHelp.KADBSQLiteHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("往RelationKeywords表中插入数据 -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 根据指定条件删除RelationKeywords表中数据
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int DeleteRelationKeywordsDAL(string strSql)
        {
            int iNum = 0;
            try
            {
                iNum = SEO.DBSqlHelp.KADBSQLiteHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("根据指定条件删除RelationKeywords表中数据 -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 更新RelationKeywords表中数据
        /// </summary>
        /// <param name="ks">实例化RelationKeywords类</param>
        /// <returns>返回影响行数</returns>
        public int UpdateRelationKeywordsDAL(SEO.Model.RelationKeywords ks)
        {
            int iNum = 0;
            string strSql = "update RelationKeywords set is_catch=1 where key_id=" + ks.KeyID;
            try
            {
                iNum = SEO.DBSqlHelp.KADBSQLiteHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("更新RelationKeywords表中数据 -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 绑定表RelationKeywords到DataGridView
        /// <param name="dgv">DataGridView控件名称</param>
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        public void BindRelationKeywordsDAL(System.Windows.Forms.DataGridView dgv, string strSql)
        {
            try
            {
                //清除最后自动生成的行
                dgv.AutoGenerateColumns = false;
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.KADBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataAdapter da = new SQLiteDataAdapter(cm);
                DataSet ds = new DataSet();
                da.Fill(ds, "RelationKeywords");
                ds.Tables["RelationKeywords"].Columns.Add("ViewCatch", typeof(string));
                for (int i = 0; i < ds.Tables["RelationKeywords"].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables["RelationKeywords"].Rows[i];
                    string strCatch = dr["is_catch"].ToString();
                    if (strCatch == "1")
                    {
                        dr["ViewCatch"] = "已分析";
                    }
                    else
                    {
                        dr["ViewCatch"] = "未分析";
                    }
                }
                dgv.DataSource = ds.Tables["RelationKeywords"].DefaultView;
                da.Dispose();
                cn.Close();
                //清除当前选择行行为
                dgv.CurrentCell = null;

            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定RelationKeywords -> " + ex.Message);
            }
        }

        /// <summary>
        /// 获取表中最大ID，并自增1
        /// </summary>
        /// <returns>返回表中最大ID，并自增1</returns>
        public int GetAutoAddNum()
        {
            int iNum = 0;
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.KADBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                string strSql = "select key_id from RelationKeywords order by key_id desc limit 1";
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                if (dr.Read())
                {
                    iNum = int.Parse(dr["key_id"].ToString()) + 1;
                }
                else
                {
                    iNum = 1;
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定RelationKeywords -> " + ex.Message);
            }

            return iNum;
        }

        /// <summary>
        /// 检测是否已存在数据库中，屏蔽已抓取的数据
        /// </summary>
        /// <param name="strKeywordsName">关键词</param>
        /// <returns>返回 True or False</returns>
        public bool CheckKeywordsDayRankIsExist(string strKeywordsName)
        {
            bool bValue = false;
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.KADBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                string strSql = "select key_id from RelationKeywords where key_name='" + strKeywordsName + "'";
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                if (dr.Read())
                {
                    bValue = true;
                }
                else
                {
                    bValue = false;
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("检测是否已存在数据库中 -> " + ex.Message);
            }

            return bValue;
        }

    }
}
